var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var wxUtil = require('../../utils/wxUtil.js');
var shopInfo = require('../../components/shopInfo/index.js');
var common = require('../../utils/common.js');
var objData = {};
var isInit = true; //是否是初始化页面

Page({
    data: {
        dataLoaded:false,
        userId: '',
        isFenxiao: null,
        isLoginShow: false,
        shareImg:''
    },

    /**
     * 页面加载
     */
    onLoad: function(options) {
        var _self = this;

        options = util.parseOptions(options);
        isInit = true;
        common.initApp({
            extSuc: function() {},
            loginSuc: function() {

                _self._recommend = (options.recommend || options.u || app.getRecommend());

                app.setRecommend(_self._recommend);

                _self._isLogin = !!app.getUserSid();

                _self._userId = app.getUserId();

                _self.setData({
                    isFenxiao: app.getIsFenxiao()
                })
                _self.getShareImg();
            }
        });

    },

    onShow: function() {
        if (isInit) {
            isInit = false;
            return;
        }

        var _self = this;

        _self._isLogin = !!app.getUserSid();
        _self._userId = app.getUserId();
        _self.setData({
            isFenxiao: app.getIsFenxiao()
        });
    },

    onLoginSuc() {
        this._isLogin = true;
    },

    _showLogin() {
        this.setData({
            isLoginShow: true
        });
    },

    _applyFx(evt) {
        var _self = this;
        var formId = evt.detail.formId;

        if (_self._isLogin) {
            util.confirm('您确定申请成为分销吗？', function() {
                ajax.P('/openapi/CBrand/brandRecommend', {
                    recommend: _self._recommend
                }, function(data) {
                    console.log(data.msg);
                    util.alert(data.msg, function() {
                        wx.switchTab({
                            url: '/pages/mall/mall'
                        });
                    });
                });

            });

        } else {
            this._showLogin();
            return;
        }
        common.submitForm(formId);
    },
    getShareImg() {
        var url = '/openapi/CHome/fxInvite';
        ajax.G(url, {}, (data) => {
            this.setData({
                shareImg:data.result.fx_img_url,
                dataLoaded:true
            })
        })
    }
})